package com.raising.modules.dic.controller;

import com.raising.modules.dic.utils.RegionUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.bind.annotation.*;

import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;
import com.raising.modules.sys.utils.DictUtils;

import com.raising.utils.JStringUtils;

import com.raising.modules.dic.entity.DicRegion;
import com.raising.modules.dic.service.DicRegionService;

import java.util.List;

/**
 * 行政区划字典表 控制器
 * @author gaoy
 * @createTime 2018-01-14 16:34:54
 */
@Controller
@RequestMapping("${adminPath}/dic/dicRegion")
public class DicRegionController extends BaseController {

    @Autowired
    private DicRegionService dicRegionService;

    /**
    * 跳转至列表页面
    * @author gaoy
    * @createTime 2018-01-14 16:34:54
    */
    @RequiresPermissions("dic:dicRegion:view")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model) throws Exception{
        olog("进入行政区划管理页面");
        List<DicRegion> regionList = dicRegionService.getList(new DicRegion());
        model.addAttribute("regionList",regionList);
        return "dic/dicRegion/list";
    }

    /**
    * 加载列表分页数据
    * @author gaoy
    * @createTime 2018-01-14 16:34:54
    */
    @RequiresPermissions("dic:dicRegion:view")
    @RequestMapping(value = "/getPage.json", method = RequestMethod.POST)
    public @ResponseBody ResultVo getPage(Model model, DicRegion dicRegion,Page<DicRegion> page) throws Exception{
        page.setEntity(dicRegion);
        page.setOrderBy(" a.id asc ");
        return ResultVo.success().data(dicRegionService.getPage(page));
    }

    /**
    * 跳转新增页面
    * @author gaoy
    * @createTime 2018-01-14 16:34:54
    */
    @RequiresPermissions("dic:dicRegion:create")
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create(Model model) {
        model.addAttribute("dicRegion", new DicRegion());
        // 是否可用
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));
        return "dic/dicRegion/add";
    }

    /**
    * 执行新增操作
    * @author gaoy
    * @createTime 2018-01-14 16:34:54
    */
    @RequiresPermissions("dic:dicRegion:create")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String create(DicRegion dicRegion, RedirectAttributes redirectAttributes) throws Exception{
        dicRegionService.save(dicRegion);
        olog("新增行政区划["+ dicRegion.getRegionNm() +"]",dicRegion.toString());
        addMessage(redirectAttributes,"新增成功");
        return "redirect:"+ adminPath +"/dic/dicRegion/update?id="+dicRegion.getId();
    }
    /**
    * 跳转更新页面
    * @author gaoy
    * @createTime 2018-01-14 16:34:54
    */
    @RequiresPermissions("dic:dicRegion:update")
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(String id, Model model) throws Exception{
        model.addAttribute("dicRegion", dicRegionService.get(id));
        // 是否可用
        model.addAttribute("yn", DictUtils.getDictList("DIM_YN"));
        return "dic/dicRegion/edit";
    }
    /**
    * 执行更新操作
    * @author gaoy
    * @createTime 2018-01-14 16:34:54
    */
    @RequiresPermissions("dic:dicRegion:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(DicRegion dicRegion, RedirectAttributes redirectAttributes) throws Exception{
        dicRegionService.save(dicRegion);
        olog("修改行政区划["+ dicRegion.getRegionNm() +"]",dicRegion.toString());
        addMessage(redirectAttributes,"修改成功");
        return "redirect:"+ adminPath +"/dic/dicRegion/update?id="+dicRegion.getId();
    }
    /**
    * 执行删除操作
    * @author gaoy
    * @createTime 2018-01-14 16:34:54
    */
    @RequiresPermissions("dic:dicRegion:delete")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(String id,int pageNo,int pageSize, RedirectAttributes redirectAttributes) throws Exception{
        DicRegion entity = dicRegionService.get(id);
        dicRegionService.delete(id);
        if(entity != null ){
            olog("删除行政区划["+ entity.getRegionNm() +"]",entity.toString());
        }
        addMessage(redirectAttributes,"删除成功");
        return "redirect:"+ adminPath +"/dic/dicRegion/list?pageNo="+pageNo+"&pageSize="+pageSize;
    }

    /**
     * 刷新行政区划缓存
     * @author gaoy
     * @createTime 2018-01-14 16:34:54
     */
    @RequiresPermissions("dic:dicRegion:cache")
    @RequestMapping(value = "/refreshCache.json", method = RequestMethod.POST)
    public @ResponseBody ResultVo refreshCache(Model model) {
        olog("刷新行政区划缓存");
        //清空缓存就是刷新
        RegionUtil.removeCache();
        return ResultVo.success().desc("成功刷新缓存");
    }
}
